package cn.iocoder.yudao.module.promotion.job.yearcardorder;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.promotion.service.yearcardorder.YearCardOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 年卡会员送券接口
 */
@Slf4j
@Component
public class YearCardMemberCouponSendJob implements JobHandler {

    @Resource
    private YearCardOrderService yearCardOrderService;

    private DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    @TenantJob
    public String execute(String param) {
        log.info("开始执行年卡会员送券任务, prams:{}", param);
        LocalDate sendDate = LocalDate.now();
        if (StringUtils.isNotBlank(param)) {
            try {
                sendDate = LocalDate.parse(param, format);
            } catch (Exception e) {
                log.error("日期格式错误，请输入yyyy-MM-dd格式日期");
            }
        }
        int count = yearCardOrderService.sendCoupon(sendDate);
        String result = StrUtil.format("年卡会员送券数量 {} 个", count);
        log.info("执行年卡会员送券任务结束, result:{}", result);
        return result;
    }
}
